package com.imiku.service;

import com.alibaba.fastjson.JSONObject;
import com.imiku.model.BlogInfo;
import com.imiku.model.CommentInfo;
import com.imiku.repository.BlogInfoRepository;
import com.imiku.utils.JsonUtils;
import com.imiku.utils.UUIDUtils;
import com.imiku.vo.BlogVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.imiku.repository.CommentInfoRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BlogService {

    @Autowired
    private BlogInfoRepository blogInfoRepository;
    @Autowired
    private CommentInfoRepository commentInfoRepository;

    public Page<BlogInfo> findBlogs(BlogVo blogVo) {
        Pageable pageable = PageRequest.of(blogVo.getPageNumber(), blogVo.getPageSize(), Sort.by(Sort.Direction.DESC, "createDate"));
        //Page<BlogInfo> postIn = blogInfoRepository.findByTypeIdAndBlogTitle(blogVo.getTypeId(), blogVo.getBlogTitle(), pageable);
        Page<BlogInfo> postIn = blogInfoRepository.findAll((Specification<BlogInfo>) (root, query, cb) -> {
            List<Predicate> predicatesList = new ArrayList<>();
            if (blogVo.getTypeId() != null) {
                Predicate namePredicate = cb.equal(root.get("typeId").as(Integer.class), blogVo.getTypeId());
                predicatesList.add(namePredicate);
            }
            if (StringUtils.isNoneBlank(blogVo.getBlogTitle())) {
                Predicate namePredicate = cb.like(root.get("blogTitle").as(String.class),"%" + blogVo.getTypeId() + "%");
                predicatesList.add(namePredicate);
            }
            Predicate[] predicates = new Predicate[predicatesList.size()];
            return cb.and(predicatesList.toArray(predicates));
        }, pageable);
        return postIn;
    }

    public BlogInfo queryBlogById(String blogId) {
        BlogInfo blogInfo = blogInfoRepository.findById(blogId).get();
        List<CommentInfo> list = commentInfoRepository.findByBlogId(blogId);
        blogInfo.setCommentList(list);
        return blogInfo;

    }

    public String update(BlogInfo blogInfo) throws UnsupportedEncodingException {
        blogInfo.setCreateDate(new Date());
        if (StringUtils.isNotBlank(blogInfo.getBlogContext())) {
            blogInfo.setBlogContext(URLDecoder.decode(blogInfo.getBlogContext(), "UTF-8"));
        }
        blogInfoRepository.save(blogInfo);
        JSONObject jsonObject = JsonUtils.getJsonObject(1, "成功");
        return jsonObject.toJSONString();
    }


    public String save(BlogInfo blogInfo) {
        blogInfo.setBlogId(UUIDUtils.getUUID36());
        blogInfo.setCreateDate(new Date());
        blogInfoRepository.save(blogInfo);
        JSONObject jsonObject = JsonUtils.getJsonObject(1, "成功");
        return jsonObject.toJSONString();
    }
}
